package com.premedsidekick;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.ScrollView;
import android.widget.TextView;

public class DisclaimerActivity extends Activity {
	AlertDialog alert;
	SharedPreferences preferences;
	Editor editor;
	Intent intent;
	int check; 
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		
		preferences = getSharedPreferences("user_settings", 0);
		editor = preferences.edit();
		final int state = preferences.getInt("accepted", 0);
		check = state;
		
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		//builder
		LayoutInflater inflater = LayoutInflater.from(this);
		ScrollView view = (ScrollView)inflater.inflate(R.layout.disclaimer, null);
		String message = getResources().getString(R.string.disclaimer);
		CharSequence sequence = Html.fromHtml(message);
		builder.setTitle("Disclaimer");
		TextView textview = (TextView)view.findViewById(R.id.textView);
		textview.setText(sequence);
		//builder.setMessage(sequence);
		builder.setCancelable(true);

		if (state < 2) {
			builder.setPositiveButton("I Agree", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					final int count = state + 1;
					editor.putInt("accepted", count);
					editor.commit();
					alert.dismiss();
					intent = new Intent(DisclaimerActivity.this, HomePageActivity.class);
					startActivity(intent);
				}
			});
		}
		builder.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				if (state >= 2) {
					alert.dismiss();
					intent = new Intent(DisclaimerActivity.this, HomePageActivity.class);
					startActivity(intent);
				}
				else 
					alert.dismiss();
			}
			
		});
		builder.setView(view);
		builder.setIcon(null);
		
		alert = builder.create();
		//alert_state = true;
		alert.show();
	}
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{ // this event takes any action anywhere on the screen upon touch_up similar to releasing on a click
		if (check >= 2)
	    	if (event.getAction() == (MotionEvent.ACTION_UP))
	    		alert.dismiss(); // replace with removing the pop-up or returning to the activity.this
			return true;
	}
	
}
